//
//  SceneDelegate.m
//  ClockIn v2
//
//  Created by Tomy on 12/11/2020.
//

#import "SceneDelegate.h"
#import "AppDelegate.h"
#import "FindViewController.h"
#import "UserViewController.h"
#import "ClockInViewController.h"
#import "Utility.h"


@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    // TabBar中的三个子页面
    FindViewController *fvc = [[FindViewController alloc]init];
    ClockInViewController *civc = [[ClockInViewController alloc]init];
    UserViewController *uvc = [[UserViewController alloc]init];

    self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
    
    UIWindowScene *windowScene = (UIWindowScene*)scene;
    CGFloat statusBarHeight = windowScene.statusBarManager.statusBarFrame.size.height;
    
    // 给三个页面设置导航栏
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:fvc];
    [Utility     setTopBarWidth:nav1.navigationBar.frame.size.height + statusBarHeight];
    nav1.tabBarItem.title = @"发现";
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:civc];
    nav2.tabBarItem.title = @"打卡";
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:uvc];
    nav3.tabBarItem.title = @"我的";

    // 导航栏设置Icon
    UIImage *img1 = [[UIImage imageNamed:@"home1.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    UIImage *img2 = [[UIImage imageNamed:@"home.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    UIImage *scaledImg1 = [self ImageZoom:img1];
    UIImage *scaledImg2 = [self ImageZoom:img2];
    nav1.tabBarItem.image = scaledImg1;
    nav1.tabBarItem.selectedImage = scaledImg2;
    
    img1 = [[UIImage imageNamed:@"note1.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    img2 = [[UIImage imageNamed:@"note.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    scaledImg1 = [self ImageZoom:img1];
    scaledImg2 = [self ImageZoom:img2];
    nav2.tabBarItem.image = scaledImg1;
    nav2.tabBarItem.selectedImage = scaledImg2;
    
    img1 = [[UIImage imageNamed:@"user1.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    img2 = [[UIImage imageNamed:@"user.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    scaledImg1 = [self ImageZoom:img1];
    scaledImg2 = [self ImageZoom:img2];
    nav3.tabBarItem.image = scaledImg1;
    nav3.tabBarItem.selectedImage = scaledImg2;

    UITabBarController *utbc = [[UITabBarController alloc]init];
    utbc.viewControllers = @[nav1, nav2, nav3];

    
    self.window.rootViewController = utbc;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
}


- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}


- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}


- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}


- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}

- (UIImage *) ImageZoom:(UIImage *)image {
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * 0.18, image.size.height * 0.18));
    [image drawInRect:CGRectMake(0, 0, image.size.width * 0.18, image.size.height * 0.18)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

@end
